package cn.security.auth.controller;

import cn.security.auth.common.Result;
import cn.security.auth.entity.LoginParam;
import cn.security.auth.service.LoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jdk.nashorn.internal.ir.annotations.Ignore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/auth")
@RestController
@Api(tags = "用户认证", description = "用户认证控制器")
public class AuthController {

    @Autowired
    private LoginService loginService;

    @ApiOperation(value = "打招呼", notes = "测试接口", httpMethod = "GET")
    @GetMapping(value = "/hello", produces = "application/json;charset=utf-8")
    public Result sayHello() {
        return Result.success("你好，Hello");
    }


    @ApiOperation(value = "获取用户信息", notes = "获取用户信息", httpMethod = "GET")
    @GetMapping(value = "/getUserInfo", produces = "application/json;charset=utf-8")
    public Result getUserInfo() {
        return Result.success(SecurityContextHolder.getContext().getAuthentication().getPrincipal());
    }

    @ApiOperation(value = "登录", notes = "登录", httpMethod = "POST")
    @PostMapping(value = "/login", produces = "application/json;charset=utf-8")
    public Result login(@RequestBody LoginParam param) throws Exception {
        return loginService.loginAndSetHeader(param);
    }
}
